package org.example.spring.service.userServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.spring.context.BaseContext;
import org.example.spring.domain.dto.OrderDTO;
import org.example.spring.domain.dto.OrderDetailDTO;
import org.example.spring.mapper.FlowerMapper;
import org.example.spring.mapper.OrderMapper;
import org.example.spring.domain.pojo.Order;
import org.example.spring.domain.pojo.OrderDetail;
import org.example.spring.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private FlowerMapper flowerMapper;

    @Override
    public ArrayList<Order> getOrderByStatus(Integer status) {
        ArrayList<Order> orders = orderMapper.getOrderByStatus(status);
        return orders;
    }

    @Override
    public void updateOrderStatus(Integer orderId, Integer status) {
        orderMapper.updateOrderStatus(orderId, status);
    }

    @Override
    public ArrayList<OrderDetailDTO> getOrderDetailByOrderId(Integer orderId) {
        ArrayList<OrderDetail> orderDetails = orderMapper.getOrderDetailByOrderId(orderId);
        ArrayList<String> flowerNames = new ArrayList<>();
        for(OrderDetail orderDetail : orderDetails) {
            String name=flowerMapper.getFlowerNameById(orderDetail.getFlowerId());
            flowerNames.add(name);
        }
        ArrayList<OrderDetailDTO> orderDetailDTOS = new ArrayList<>();
        for (int i = 0; i < orderDetails.size(); i++) {
            OrderDetail orderDetail = orderDetails.get(i);
            String flowerName = flowerNames.get(i);
            OrderDetailDTO orderDetailDTO = OrderDetailDTO.builder()
                    .orderDetailId(orderDetail.getOrderDetailId())
                    .orderId(orderDetail.getOrderId())
                    .flowerId(orderDetail.getFlowerId())
                    .quantity(orderDetail.getQuantity())
                    .price(orderDetail.getPrice())
                    .flowerName(flowerName)
                    .build();
            orderDetailDTOS.add(orderDetailDTO);
        }
        return orderDetailDTOS;
    }

    @Override
    public Integer addOrder(Order order) {
        orderMapper.addOrder(order);
        Integer orderId = order.getOrderId();
        return orderId;
    }

    @Override
    public OrderDTO getOrdersByPage(OrderDTO orderDTO) {
        PageHelper.startPage(orderDTO.getPage(), orderDTO.getPageSize());
        Page<Order> page = orderMapper.getOrdersByPage(orderDTO);
        ArrayList<Order> orders = new ArrayList<>();
        for (Order order : page) {
            orders.add(order);
        }
        int total = (int) page.getTotal();
        OrderDTO od = new OrderDTO();
        od.setOrders(orders);
        od.setTotal(total);
        return od;
    }

    @Override
    public List<Order> list(){
        Integer userId = BaseContext.getCurrentId();
        return orderMapper.list(userId);
    }
}
